package com.huike.clues.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.clues.domain.TbCourse;
import com.huike.clues.domain.dto.TbCourseDTO;
import com.huike.clues.mapper.TbCourseMapper;
import com.huike.clues.service.TbCourseService;
import com.huike.common.constant.Constants;
import com.huike.common.utils.uuid.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 93238
 * @description 针对表【tb_course(课程管理)】的数据库操作Service实现
 * @createDate 2023-10-12 06:35:46
 */
@Service
public class TbCourseServiceImpl extends ServiceImpl<TbCourseMapper, TbCourse> implements TbCourseService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 新增
     *
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(TbCourse dto) {
        TbCourse course = BeanUtil.toBean(dto, TbCourse.class);
        String code = UUIDUtils.getUUID();
        course.setCode(code);
        course.setIsDelete(0);
        this.save(course);
        redisTemplate.delete(Constants.CLUES_COURSE_LISTSELECT);
    }

    /**
     * 分页查询课程管理列表
     *
     * @param dto
     * @return
     */
    @Override
    public List<TbCourse> pageList(TbCourseDTO dto) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<TbCourse> list = this.lambdaQuery()
                .eq(StrUtil.isNotBlank(dto.getApplicablePerson()), TbCourse::getApplicablePerson, dto.getApplicablePerson())
                .like(StrUtil.isNotBlank(dto.getCode()), TbCourse::getCode, dto.getCode())
                .like(StrUtil.isNotBlank(dto.getName()), TbCourse::getName, dto.getName())
                .eq(StrUtil.isNotBlank(dto.getSubject()), TbCourse::getSubject, dto.getSubject())
                .eq(TbCourse::getIsDelete, 0)
                .between(dto.getParams().get("beginCreateTime") != null && dto.getParams().get("beginCreateTime") != ""
                                && dto.getParams().get("endCreateTime") != null && dto.getParams().get("endCreateTime") != "",
                        TbCourse::getCreateTime, dto.getParams().get("beginCreateTime"), dto.getParams().get("endCreateTime"))
                .orderByDesc(TbCourse::getCreateTime)
                .list();
        return list;
    }


    /**
     * 修改
     *
     * @param course
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCourse(TbCourse course) {
        this.updateById(course);
        redisTemplate.delete(Constants.CLUES_COURSE_LISTSELECT);
    }

    /**
     * 课程下拉列表
     */
    @Override
    public List<TbCourse> listSelect(String subject) {
        String courseListJson = redisTemplate.opsForValue().get(Constants.CLUES_COURSE_LISTSELECT);

        if (StrUtil.isNotBlank(courseListJson)) {
            return JSONUtil.toList(courseListJson, TbCourse.class);
        }
        List<TbCourse> list = this.lambdaQuery()
                .eq(TbCourse::getSubject, subject)
                .eq(TbCourse::getIsDelete, 0)
                .list();
        redisTemplate.opsForValue().set(Constants.CLUES_COURSE_LISTSELECT, JSONUtil.toJsonStr(list));
        return list;
    }

    /**
     * 删除课程管理
     */
    @Override
    public void deleteByIds(ArrayList<Long> ids) {
        this.lambdaUpdate()
                .in(TbCourse::getId, ids)
                .set(TbCourse::getIsDelete, 1)
                .update();
        redisTemplate.delete(Constants.CLUES_COURSE_LISTSELECT);
    }
}




